import type {
  AnalyticsTrendsResponse,
  OccupancyListResponse,
  ResourceListResponse,
  ResourceOverviewResponse,
  ResourceQueryParams,
  ScheduleListResponse,
  ScheduleQueryParams,
  ResourceItem,
  ResourceTreeItem,
} from './types';

import { mockRequestClient as requestClient } from '#/api/request';

// 资源概览
export function getResourceOverview(): Promise<ResourceOverviewResponse> {
  return requestClient.get('/resource-center/overview');
}

// 业务资源目录
export function getBusinessResources(
  params: ResourceQueryParams,
): Promise<ResourceListResponse> {
  return requestClient.get('/resource-center/catalog/business', { params });
}

// 技术资源目录
export function getTechnicalResources(
  params: ResourceQueryParams,
): Promise<ResourceListResponse> {
  return requestClient.get('/resource-center/catalog/technical', { params });
}

// 调度列表
export function getScheduleList(
  params: ScheduleQueryParams,
): Promise<ScheduleListResponse> {
  return requestClient.get('/resource-center/scheduling/list', { params });
}

// 占用记录
export function getOccupancyList(params: any): Promise<OccupancyListResponse> {
  return requestClient.get('/resource-center/occupancy/list', { params });
}

// 分析趋势
export function getAnalyticsTrends(): Promise<AnalyticsTrendsResponse> {
  return requestClient.get('/resource-center/analytics/trends');
}

// 资源目录相关API
export function getResourceCategories(params?: any) {
  return requestClient.get('/resource-center/categories', { params });
}

export function createResourceCategory(data: any) {
  return requestClient.post('/resource-center/categories', data);
}

export function updateResourceCategory(id: number, data: any) {
  return requestClient.put(`/resource-center/categories/${id}`, data);
}

export function deleteResourceCategory(id: number) {
  return requestClient.delete(`/resource-center/categories/${id}`);
}

export function getResources(params?: any) {
  return requestClient.get('/resource-center/resources', { params });
}

// 导出类型
export type { ResourceItem, ResourceTreeItem } from './types';

export function createResource(data: any) {
  return requestClient.post('/resource-center/resources', data);
}

export function updateResource(id: number, data: any) {
  return requestClient.put(`/resource-center/resources/${id}`, data);
}

export function deleteResource(id: number) {
  return requestClient.delete(`/resource-center/resources/${id}`);
}

export function getResourceStats(params?: any) {
  return requestClient.get('/resource-center/resources/stats', { params });
}

// 资源状态管理API
export function getResourceStates(resourceId: number) {
  return requestClient.get(`/resource-center/resources/${resourceId}/states`);
}

export function updateResourceState(resourceId: number, stateData: any) {
  return requestClient.put(
    `/resource-center/resources/${resourceId}/states`,
    stateData,
  );
}

// 资源事件管理API
export function getResourceEvents(resourceId: number) {
  return requestClient.get(`/resource-center/resources/${resourceId}/events`);
}

export function triggerResourceEvent(resourceId: number, eventData: any) {
  return requestClient.post(
    `/resource-center/resources/${resourceId}/events`,
    eventData,
  );
}

// 资源方法管理API
export function getResourceMethods(resourceId: number) {
  return requestClient.get(`/resource-center/resources/${resourceId}/methods`);
}

export function executeResourceMethod(resourceId: number, methodData: any) {
  return requestClient.post(
    `/resource-center/resources/${resourceId}/methods`,
    methodData,
  );
}

// 资源委托管理API
export function getResourceDelegations(resourceId: number) {
  return requestClient.get(
    `/resource-center/resources/${resourceId}/delegations`,
  );
}

export function createResourceDelegation(
  resourceId: number,
  delegationData: any,
) {
  return requestClient.post(
    `/resource-center/resources/${resourceId}/delegations`,
    delegationData,
  );
}

// 资源标签管理API
export function getResourceTags(params?: any) {
  return requestClient.get('/resource-center/tags', { params });
}

export function createResourceTag(data: any) {
  return requestClient.post('/resource-center/tags', data);
}

export function updateResourceTag(id: number, data: any) {
  return requestClient.put(`/resource-center/tags/${id}`, data);
}

export function deleteResourceTag(id: number) {
  return requestClient.delete(`/resource-center/tags/${id}`);
}

export function assignResourceTag(resourceId: number, tagData: any) {
  return requestClient.post(
    `/resource-center/resources/${resourceId}/tags`,
    tagData,
  );
}

export function removeResourceTag(resourceId: number, tagId: number) {
  return requestClient.delete(
    `/resource-center/resources/${resourceId}/tags/${tagId}`,
  );
}

// 用户个性化API
export function getUserPersonalizations(userId: number, params?: any) {
  return requestClient.get(
    `/resource-center/users/${userId}/personalizations`,
    { params },
  );
}

export function createUserPersonalization(userId: number, data: any) {
  return requestClient.post(
    `/resource-center/users/${userId}/personalizations`,
    data,
  );
}

export function updateUserPersonalization(
  userId: number,
  id: number,
  data: any,
) {
  return requestClient.put(
    `/resource-center/users/${userId}/personalizations/${id}`,
    data,
  );
}

export function deleteUserPersonalization(userId: number, id: number) {
  return requestClient.delete(
    `/resource-center/users/${userId}/personalizations/${id}`,
  );
}

// 资源调度管理
export function getSchedules(
  params: ScheduleQueryParams,
): Promise<ScheduleListResponse> {
  return requestClient.get('/resource-center/schedules', { params });
}

export function createSchedule(data: any) {
  return requestClient.post('/resource-center/schedules', data);
}

export function updateSchedule(id: string, data: any) {
  return requestClient.put(`/resource-center/schedules/${id}`, data);
}

export function deleteSchedule(id: string) {
  return requestClient.delete(`/resource-center/schedules/${id}`);
}

export function updateScheduleStatus(id: string, status: string) {
  return requestClient.put(`/resource-center/schedules/${id}/status`, {
    status,
  });
}

// 智能调度
export function getIntelligentSchedule(params: any) {
  return requestClient.post('/resource-center/schedules/intelligent', params);
}

export function getConflictDetection(params: any) {
  return requestClient.get('/resource-center/schedules/conflicts', { params });
}

// 资源使用记录
export function getResourceUsage(params: any) {
  return requestClient.get('/resource-center/usage', { params });
}

export function createResourceUsage(data: any) {
  return requestClient.post('/resource-center/usage', data);
}

// 资源维护管理
export function getMaintenanceList(params: any) {
  return requestClient.get('/resource-center/maintenance', { params });
}

export function createMaintenance(data: any) {
  return requestClient.post('/resource-center/maintenance', data);
}

export function updateMaintenance(id: string, data: any) {
  return requestClient.put(`/resource-center/maintenance/${id}`, data);
}

export function deleteMaintenance(id: string) {
  return requestClient.delete(`/resource-center/maintenance/${id}`);
}

// 资源采购管理
export function getPurchaseList(params: any) {
  return requestClient.get('/resource-center/purchase', { params });
}

export function createPurchase(data: any) {
  return requestClient.post('/resource-center/purchase', data);
}

export function updatePurchase(id: string, data: any) {
  return requestClient.put(`/resource-center/purchase/${id}`, data);
}

export function deletePurchase(id: string) {
  return requestClient.delete(`/resource-center/purchase/${id}`);
}

// 跨业务资源共享
export function getCrossBusinessSharing(params: any) {
  return requestClient.get('/resource-center/sharing', { params });
}

export function createSharing(data: any) {
  return requestClient.post('/resource-center/sharing', data);
}

export function updateSharing(id: string, data: any) {
  return requestClient.put(`/resource-center/sharing/${id}`, data);
}

export function deleteSharing(id: string) {
  return requestClient.delete(`/resource-center/sharing/${id}`);
}

// 资源分析报表
export function getResourceAnalytics(params: any) {
  return requestClient.get('/resource-center/analytics', { params });
}

export function getResourceReports(params: any) {
  return requestClient.get('/resource-center/reports', { params });
}

// 资源导入导出
export function importResources(data: any) {
  return requestClient.post('/resource-center/import', data);
}

export function exportResources(params: any) {
  return requestClient.get('/resource-center/export', { params });
}

// 资源模板管理
export function getResourceTemplates(params?: any) {
  return requestClient.get('/resource-center/templates', { params });
}

export function createResourceTemplate(data: any) {
  return requestClient.post('/resource-center/templates', data);
}

export function updateResourceTemplate(id: string, data: any) {
  return requestClient.put(`/resource-center/templates/${id}`, data);
}

export function deleteResourceTemplate(id: string) {
  return requestClient.delete(`/resource-center/templates/${id}`);
}

export function getTemplateCategories() {
  return requestClient.get('/resource-center/template-categories');
}

// 资源监控告警
export function getResourceMonitoring(params: any) {
  return requestClient.get('/resource-center/monitoring', { params });
}

export function getResourceAlerts(params: any) {
  return requestClient.get('/resource-center/alerts', { params });
}

export function updateAlertSettings(data: any) {
  return requestClient.put('/resource-center/alerts/settings', data);
}

// 调度规则管理API
export function getSchedulingRules(params?: any) {
  return requestClient.get('/resource-center/settings/rules', { params });
}

export function createSchedulingRule(data: any) {
  return requestClient.post('/resource-center/settings/rules', data);
}

export function updateSchedulingRule(id: string, data: any) {
  return requestClient.put(`/resource-center/settings/rules/${id}`, data);
}

export function deleteSchedulingRule(id: string) {
  return requestClient.delete(`/resource-center/settings/rules/${id}`);
}

export function toggleSchedulingRuleStatus(id: string, status: string) {
  return requestClient.put(`/resource-center/settings/rules/${id}/status`, {
    status,
  });
}

// 分类资源管理API
export function getCategoryResources(categoryId: string, params?: any) {
  return requestClient.get(
    `/resource-center/categories/${categoryId}/resources`,
    { params },
  );
}

export function addResourceToCategory(
  categoryId: string,
  resourceIds: string[],
) {
  return requestClient.post(
    `/resource-center/categories/${categoryId}/resources`,
    { resourceIds },
  );
}

export function removeResourceFromCategory(
  categoryId: string,
  resourceIds: string[],
) {
  return requestClient.delete(
    `/resource-center/categories/${categoryId}/resources`,
    {
      data: { resourceIds },
    },
  );
}

export function getAvailableResources(params?: any) {
  return requestClient.get('/resource-center/resources/available', { params });
}

// 模板版本管理API
export function getTemplateVersions(templateId: string) {
  return requestClient.get(`/resource-center/templates/${templateId}/versions`);
}

export function createTemplateVersion(templateId: string, data: any) {
  return requestClient.post(
    `/resource-center/templates/${templateId}/versions`,
    data,
  );
}

export function rollbackTemplateVersion(templateId: string, versionId: string) {
  return requestClient.post(
    `/resource-center/templates/${templateId}/versions/${versionId}/rollback`,
  );
}

// 模板应用API
export function applyTemplateToResources(
  templateId: string,
  resourceIds: string[],
) {
  return requestClient.post(`/resource-center/templates/${templateId}/apply`, {
    resourceIds,
  });
}

export function getTemplateUsage(templateId: string) {
  return requestClient.get(`/resource-center/templates/${templateId}/usage`);
}

// 能力管理相关API
// 能力模板API
export function getCapabilityTemplates(params?: any) {
  return requestClient.get('/resource-center/capability-templates', {
    params,
  });
}

// 能力定义API
export function getCapabilityDefinitions(params?: any) {
  return requestClient.get('/resource-center/capability-definitions', {
    params,
  });
}

export function createCapabilityDefinition(data: any) {
  return requestClient.post('/resource-center/capability-definitions', data);
}

export function updateCapabilityDefinition(id: string, data: any) {
  return requestClient.put(
    `/resource-center/capability-definitions/${id}`,
    data,
  );
}

export function deleteCapabilityDefinition(id: string) {
  return requestClient.delete(`/resource-center/capability-definitions/${id}`);
}

// 能力实例API
export function getCapabilityInstances(params?: any) {
  return requestClient.get('/resource-center/capability-instances', { params });
}

export function createCapabilityInstance(data: any) {
  return requestClient.post('/resource-center/capability-instances', data);
}

export function updateCapabilityInstance(id: string, data: any) {
  return requestClient.put(`/resource-center/capability-instances/${id}`, data);
}

export function deleteCapabilityInstance(id: string) {
  return requestClient.delete(`/resource-center/capability-instances/${id}`);
}

// 能力执行API
export function executeCapability(data: any) {
  return requestClient.post('/resource-center/capability-executions', data);
}

export function getCapabilityExecutions(params?: any) {
  return requestClient.get('/resource-center/capability-executions', {
    params,
  });
}

// 资源属性管理API
export function getPropertySchema(params?: any) {
  return requestClient.get('/resource-center/property-schema', { params });
}

export function savePropertySchema(data: any) {
  return requestClient.post('/resource-center/property-schema', data);
}

export function getPropertyData(params?: any) {
  return requestClient.get('/resource-center/property-data', { params });
}

export function savePropertyData(data: any) {
  return requestClient.post('/resource-center/property-data', data);
}

// SaaS工厂相关API
export function getSaaSApplications(params?: any) {
  return requestClient.get('/resource-center/saas/applications', { params });
}

export function createSaaSApplication(data: any) {
  return requestClient.post('/resource-center/saas/applications', data);
}

export function updateSaaSApplication(id: string, data: any) {
  return requestClient.put(`/resource-center/saas/applications/${id}`, data);
}

export function deleteSaaSApplication(id: string) {
  return requestClient.delete(`/resource-center/saas/applications/${id}`);
}

export function deploySaaSApplication(id: string, data: any) {
  return requestClient.post(
    `/resource-center/saas/applications/${id}/deploy`,
    data,
  );
}

// 部署相关API
export function deployApplication(config: any) {
  return requestClient.post('/resource-center/saas/applications/deploy', config);
}

export function getDeployStatus(deployId: string) {
  return requestClient.get(`/resource-center/saas/applications/deploy/status/${deployId}`);
}

export function getDeployLogs(deployId: string) {
  return requestClient.get(`/resource-center/saas/applications/deploy/logs/${deployId}`);
}

export function cancelDeploy(deployId: string) {
  return requestClient.post(`/resource-center/saas/applications/deploy/cancel/${deployId}`);
}

export function rollbackDeploy(deployId: string) {
  return requestClient.post(`/resource-center/saas/applications/deploy/rollback/${deployId}`);
}

export function getSAASStatistics() {
  return requestClient.get('/resource-center/saas/statistics');
}

export function getSAASApplicationTemplates(params?: any) {
  return requestClient.get('/resource-center/saas/templates', { params });
}

export function createSAASApplicationTemplate(data: any) {
  return requestClient.post('/resource-center/saas/templates', data);
}

export function updateSAASApplicationTemplate(id: string, data: any) {
  return requestClient.put(`/resource-center/saas/templates/${id}`, data);
}

export function deleteSAASApplicationTemplate(id: string) {
  return requestClient.delete(`/resource-center/saas/templates/${id}`);
}

// 定价管理相关API
export function getPricingModels(params?: any) {
  return requestClient.get('/resource-center/pricing/models', { params });
}

export function createPricingModel(data: any) {
  return requestClient.post('/resource-center/pricing/models', data);
}

export function updatePricingModel(id: string, data: any) {
  return requestClient.put(`/resource-center/pricing/models/${id}`, data);
}

export function deletePricingModel(id: string) {
  return requestClient.delete(`/resource-center/pricing/models/${id}`);
}

export function getPricingRules(modelId: string) {
  return requestClient.get(`/resource-center/pricing/models/${modelId}/rules`);
}

export function createPricingRule(modelId: string, data: any) {
  return requestClient.post(
    `/resource-center/pricing/models/${modelId}/rules`,
    data,
  );
}

export function updatePricingRule(modelId: string, ruleId: string, data: any) {
  return requestClient.put(
    `/resource-center/pricing/models/${modelId}/rules/${ruleId}`,
    data,
  );
}

export function deletePricingRule(modelId: string, ruleId: string) {
  return requestClient.delete(
    `/resource-center/pricing/models/${modelId}/rules/${ruleId}`,
  );
}

export function calculatePrice(modelId: string, params: any) {
  return requestClient.post(
    `/resource-center/pricing/models/${modelId}/calculate`,
    params,
  );
}

export function getPricingAnalytics(params?: any) {
  return requestClient.get('/resource-center/pricing/analytics', { params });
}

// 资源市场相关API
export function getMarketListings(params?: any) {
  return requestClient.get('/resource-center/market/listings', { params });
}

export function getMarketListingDetail(id: string) {
  return requestClient.get(`/resource-center/market/listings/${id}`);
}

export function createMarketListing(data: any) {
  return requestClient.post('/resource-center/market/listings', data);
}

export function updateMarketListing(id: string, data: any) {
  return requestClient.put(`/resource-center/market/listings/${id}`, data);
}

export function deleteMarketListing(id: string) {
  return requestClient.delete(`/resource-center/market/listings/${id}`);
}

export function toggleMarketListingStatus(id: string, status: string) {
  return requestClient.put(`/resource-center/market/listings/${id}/status`, {
    status,
  });
}

export function favoriteMarketListing(id: string) {
  return requestClient.post(`/resource-center/market/listings/${id}/favorite`);
}

export function unfavoriteMarketListing(id: string) {
  return requestClient.delete(
    `/resource-center/market/listings/${id}/favorite`,
  );
}

// 资源市场分类API
export function getMarketCategories() {
  return requestClient.get('/resource-center/market/categories');
}

export function getMarketStats() {
  return requestClient.get('/resource-center/market/stats');
}

// 订单管理API
export function getOrders(params?: any) {
  return requestClient.get('/resource-center/market/orders', { params });
}

export function getOrderDetail(id: string) {
  return requestClient.get(`/resource-center/market/orders/${id}`);
}

export function createOrder(data: any) {
  return requestClient.post('/resource-center/market/orders', data);
}

export function updateOrder(id: string, data: any) {
  return requestClient.put(`/resource-center/market/orders/${id}`, data);
}

export function updateOrderStatus(id: string, status: string) {
  return requestClient.put(`/resource-center/market/orders/${id}/status`, {
    status,
  });
}

export function cancelOrder(id: string, reason?: string) {
  return requestClient.post(`/resource-center/market/orders/${id}/cancel`, {
    reason,
  });
}

// 交易管理API
export function getTransactions(params?: any) {
  return requestClient.get('/resource-center/market/transactions', { params });
}

export function getTransactionDetail(id: string) {
  return requestClient.get(`/resource-center/market/transactions/${id}`);
}

export function processPayment(orderId: string, paymentData: any) {
  return requestClient.post(
    `/resource-center/market/orders/${orderId}/payment`,
    paymentData,
  );
}

export function refundPayment(transactionId: string, refundData: any) {
  return requestClient.post(
    `/resource-center/market/transactions/${transactionId}/refund`,
    refundData,
  );
}

// 评价管理API
export function getListingReviews(listingId: string, params?: any) {
  return requestClient.get(
    `/resource-center/market/listings/${listingId}/reviews`,
    { params },
  );
}

export function createReview(listingId: string, data: any) {
  return requestClient.post(
    `/resource-center/market/listings/${listingId}/reviews`,
    data,
  );
}

export function updateReview(listingId: string, reviewId: string, data: any) {
  return requestClient.put(
    `/resource-center/market/listings/${listingId}/reviews/${reviewId}`,
    data,
  );
}

export function deleteReview(listingId: string, reviewId: string) {
  return requestClient.delete(
    `/resource-center/market/listings/${listingId}/reviews/${reviewId}`,
  );
}

// 卖家管理API
export function getSellerProfile(sellerId: string) {
  return requestClient.get(`/resource-center/market/sellers/${sellerId}`);
}

export function updateSellerProfile(sellerId: string, data: any) {
  return requestClient.put(`/resource-center/market/sellers/${sellerId}`, data);
}

export function getSellerListings(sellerId: string, params?: any) {
  return requestClient.get(
    `/resource-center/market/sellers/${sellerId}/listings`,
    { params },
  );
}

// 消息通信API
export function getConversations(params?: any) {
  return requestClient.get('/resource-center/market/conversations', { params });
}

export function getConversationMessages(conversationId: string, params?: any) {
  return requestClient.get(
    `/resource-center/market/conversations/${conversationId}/messages`,
    { params },
  );
}

export function sendMessage(conversationId: string, data: any) {
  return requestClient.post(
    `/resource-center/market/conversations/${conversationId}/messages`,
    data,
  );
}

export function createConversation(data: any) {
  return requestClient.post('/resource-center/market/conversations', data);
}
